#include "BackgroundImage.h"

BackgroundImage::BackgroundImage()
{
    m_sprite = NULL;
}

BackgroundImage::BackgroundImage(AnimatedSprite *sprite, Vector2di offset, Vector2di moveA, Vector2di forEveryB, pair<bool,bool> repeat)
{
    m_sprite = sprite;
    m_offset = offset;
    m_moveA = moveA;
    m_forEveryB = forEveryB;
    m_screenPosition = offset;
    m_repeat = repeat;
}

BackgroundImage::~BackgroundImage()
{
    delete m_sprite;
}

void BackgroundImage::updateScreenPosition(const Vector2df& mapOffset)
{
   	m_screenPosition.x = m_offset.x;
   	if ((m_moveA.x != 0) && (m_forEveryB.x != 0))
        m_screenPosition.x -= (int)((mapOffset.x * m_moveA.x) / m_forEveryB.x);
    m_screenPosition.y = m_offset.y;
   	if ((m_moveA.y != 0) && (m_forEveryB.y != 0))
        m_screenPosition.y -= (int)((mapOffset.y * m_moveA.y) / m_forEveryB.y);

    if (m_repeat.first)
    {
        // make sure it is on the screen
        while (m_screenPosition.x < ((int)m_sprite->getWidth() * -1))
            m_screenPosition.x += m_sprite->getWidth();
        // make sure it is as far to the left as possible
        while (m_screenPosition.x > 0)
            m_screenPosition.x -= m_sprite->getWidth();
    }
    if (m_repeat.second)
    {
        while (m_screenPosition.y < ((int)m_sprite->getHeight() * -1))
            m_screenPosition.y += m_sprite->getHeight();
        // make sure it is as far to the top as possible
        while (m_screenPosition.y > 0)
            m_screenPosition.y -= m_sprite->getHeight();
    }

   	if (m_sprite) m_sprite->setPosition(m_screenPosition);
}

void BackgroundImage::render() {
    if (m_sprite)
    {
        if (not (m_repeat.first or m_repeat.second))
            m_sprite->render();
        else
        {
            // This can probably be done a bit cleaner and nicer
            if (m_repeat.first)
            {
                // make it repeat (starting always at the left)
                while (m_screenPosition.x < (int)GFX::getXResolution())
                {
                    if (m_repeat.second) // repeating in both directions
                    {
                        // make it repeat (starting always at the top)
                        int startPos = m_screenPosition.y;
                        while (m_screenPosition.y < (int)GFX::getYResolution())
                        {
                            m_sprite->render();
                            m_screenPosition.y += m_sprite->getHeight();
                            m_sprite->setPosition(m_screenPosition);
                        }
                        m_screenPosition.y = startPos;
                    }
                    else
                        m_sprite->render();
                    m_screenPosition.x += m_sprite->getWidth();
                    m_sprite->setPosition(m_screenPosition);
                }
            }
            else if (m_repeat.second)
            {
                // make it repeat (starting always at the top)
                while (m_screenPosition.y < (int)GFX::getYResolution())
                {
                    m_sprite->render();
                    m_screenPosition.y += m_sprite->getHeight();
                    m_sprite->setPosition(m_screenPosition);
                }
            } // if repeat
        } // else
    }
}

